<?php
class Arm_View_Helper_FckEditor extends Zend_View_Helper_Abstract
{
    
    protected $_width = '90%';
    
    protected $_height = '400px';

    protected $_baseConfig = '/scripts/fckeditor/config_fckeditor/';
    
    
    public function fckEditor($name, $value, $attr = array())
    {
        $value = $this->view->escape($value);
        // include js & css for rte
        $this->view->headScript()->appendFile('/scripts/fckeditor/fck/fckeditor.js');
        $attr['BasePath'] = '/scripts/fckeditor/fck/';
        $attr['SkinPath'] = $attr['BasePath'] . 'editor/skins/office2003/';
        $attr = $this->_size($attr);
        
        // Смотрим указан ли конфиг в опциях
        if(array_key_exists('typeConfig', $attr)) {
            $attr['CustomConfigurationsPath'] = $this->_baseConfig . $attr['typeConfig'];
            unset($attr['typeConfig']);
        } else {
            $attr['CustomConfigurationsPath'] = $this->_baseConfig . 'full.js';
        }
        
        if(is_file(Arm_Core::getPathPublicHtml() . ltrim($attr['CustomConfigurationsPath'], '/')) === false) {
            throw new Arm_Exception(Arm_Translate::_('Указан не существующий файл конфига "%s"', $attr['CustomConfigurationsPath']));
        }
        
        return $this->_script($name, $attr, $value);
    }
    
    /**
     * Перезаписываем существующие поле textera.
     */
    protected function _script($name, array $options, $value)
    {
     $res  = '<textarea id="' . $name . '" name="' . $name . '">' . $value . '</textarea>';
     $res .= '<script type="text/javascript">
        var XFCKeditor = new FCKeditor(\'' . $name . '\');' . PHP_EOL;
        foreach ($options as $k=>$v) {
            if($k === 'CustomConfigurationsPath') {
                $res .= 'XFCKeditor.Config["' . $k . '"] = "' . $v . '";' . PHP_EOL;
                continue;
            }
          $res .= 'XFCKeditor.' . $k . ' = "' . $v . '";' . PHP_EOL;
        }
          $res .= 'XFCKeditor.ReplaceTextarea();';
        //$res .='XFCKeditor.Create();
          $res .= '</script>';
     return $res;
    }
    
    /**
     * размер редактора.
     */
    protected function _size($attr)
    {
        
        if(array_key_exists('height', $attr)) {
            $attr['Height'] = $attr['height'];
            unset($attr['height']);
        }
        
        if(array_key_exists('width', $attr)) {
            $attr['Width'] = $attr['width'];
            unset($attr['width']);
        }
        
        if(false === array_key_exists('Width', $attr)) {
            $attr['Width'] = $this->_width;
        }
        
        if(false === array_key_exists('Height', $attr)) {
            $attr['Height'] = $this->_height;
        }
        
        $width  = $attr['Width'];
        $height = $attr['Height'];
        
        if(false === strpos($width, '%') and false === strpos($width, 'px')) {
            $width  .='px';
        }
        if(false === strpos($height, '%') and false === strpos($height, 'px')) {
            $height .='px';
        }
        
        $attr['Height']   = $height;
        $attr['Width']    = $width;
        return $attr;
    }
}
